<?php


namespace app\user\controller;


use app\common\controller\Base;
use app\common\model\HouseNature;
use app\common\model\UserHouse as houseModel;
use think\Request;
use app\common\lib\Util;

class House extends Base
{
    //房产列表
    public function index(houseModel $houseModel)
    {
        $houseList = $houseModel->field('id,province,city,district,address,nature_id')
            ->with(['house_nature' => function ($query) {
                $query->field('id,name');
            }])
            ->where(['user_id' => $this->userId])->select();
        foreach ($houseList as &$v) {
            $v['full_address'] = $houseModel->full_address;
        }
        return Util::apiResponse(['house_list' => $houseList], '', 200);
    }

    //房屋性质
    public function natureList()
    {
        $natureList = HouseNature::field('id,name')->select();
        return Util::apiResponse(['nature_list' => $natureList], '', 200);
    }

    //房产的添加
    public function add(Request $request, houseModel $houseModel)
    {
        $houseOwner = $request->house_owner;
        $houseNumber = $request->house_number;
        $province = $request->province;
        $city = $request->city;
        $district = $request->district;
        $address = $request->address;
        if (empty($houseOwner) && $houseOwner != '0') {
            return Util::apiResponse([], '请填房屋所有权人', 400);
        }
        if (empty($houseNumber) && $houseNumber != '0') {
            return Util::apiResponse([], '请填写房权证编号', 400);
        }
        if (!$province || !$city || !$district || !$address) {
            return Util::apiResponse([], '请将地址填写完整', 400);
        }
        $recordTime = intval($request->record_time);
        $natureId = intval($request->nature_id);
        $exist = HouseNature::where(['id' => $natureId])->find();
        if (!$exist) {
            return Util::apiResponse([], '参数错误', 400);
        }
        $houseData = [
            'user_id' => $this->userId,
            'house_owner' => $houseOwner,
            'house_number' => $houseNumber,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'address' => $address,
            'record_time' => $recordTime,
            'nature_id' => $natureId
        ];
        $ins = $houseModel->save($houseData);
        if (!$ins) {
            return Util::apiResponse([], '操作失败', 400);
        }
        return Util::apiResponse([], '操作成功', 200);
    }

    public function detail(Request $request, houseModel $houseModel)
    {
        $id = $request->param('id');
        $houseInfo = $houseModel->field('id,province,city,district,address,nature_id')
            ->with(['house_nature' => function ($query) {
                $query->field('id,name');
            }])
            ->where(['user_id' => $this->userId,'id'=>$id])->find();
        if($houseInfo){
            $houseInfo['full_address'] = $houseModel->full_address;
        }
        return Util::apiResponse($houseInfo, '', 200);
    }

    public function delete(Request $request, houseModel $houseModel){
        $id = $request->param('id');
        $exist = $houseModel->where(['id'=>$id,'user_id'=>$this->userId])->find();
        if(!$exist){
            return Util::apiResponse([], '参数错误', 400);
        }
        $result  = $houseModel->destroy(['id'=>$id,'user_id'=>$this->userId]);
        if(!$result){
            return Util::apiResponse([], '操作失败', 400);
        }
        return Util::apiResponse([], '操作成功', 200);
    }
}